<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 文章管理 iframe 框</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <style>
        input[disabled] {
            background: #9e9e9e;
        }
    </style>
</head>
<body>
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <input type="hidden" name="id" value="{$find.id}">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名</label>
        <div class="layui-input-inline">
            <input type="text" name="role" value="{$find.role}" lay-verify="required" placeholder="角色名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色权限</label>
        <div class="layui-input-block">
            {volist name="accrss" id="vo"}
                <div>
                    <span style="background: #f7f7f7;display: inline-block;height: 40px;padding-left: 10px;"><input type="checkbox" value="{$vo.id}" lay-filter="fu" title="{$vo.power_name}" lay-skin="primary"></span>
                    <div style="margin: 10px 0;padding-left: 10px;padding-bottom: 10px;background: #f7f7f7;">
                        {volist name="vo.lower" id="voo"}
                            <span style="width: 180px;display: inline-block;"><input type="checkbox" value="{$voo.id}" title="{$voo.power_name}" lay-skin="primary"></span>
                        {/volist}
                    </div>
                </div>
            {/volist}
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="确认添加">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="确认编辑">
    </div>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function(){
        var $ = layui.$,
            form = layui.form;

        var power = "{$find.power}";
        if (power){
            var strarr = power.split(',');
            $("input[type=checkbox]").each(function (){
                var value = $(this).val();
                //是否存在
                var index = strarr.indexOf(value);
                if (index >= 0){
                    $(this).prop('checked', true);
                }
            });
            form.render();
        }

        //监听checkbox复选
        form.on('checkbox(fu)', function(data){
            var that = data.othis;
            if (data.elem.checked){
                that.parent().next().find('input[type=checkbox]').prop('checked', true);
            }else{
                that.parent().next().find('input[type=checkbox]').prop('checked', false);
            }
            form.render();
        });

        //监听提交
        form.on('submit(layuiadmin-app-form-submit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            var checked = [];
            $("input[type=checkbox]:checked").each(function (){
                var value = $(this).val();
                checked.push(value);
            });
            field.checked = checked;
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                type: "POST",
                url: "{:url('Rbac/role_add')}",
                data: {field:field},
                dataType: "json",
                success: function(e){
                    if (e == 1){
                        layer.msg('添加成功');
                        setTimeout(function () {
                            parent.layui.table.reload('demo'); //重载表格
                            parent.layer.close(index); //再执行关闭
                        }, 500)
                    }else{
                        layer.msg('添加失败');
                    }
                }
            });
        });

        //监听提交
        form.on('submit(layuiadmin-app-form-edit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            var checked = [];
            $("input[type=checkbox]:checked").each(function (){
                var value = $(this).val();
                checked.push(value);
            });
            field.checked = checked;
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                type: "POST",
                url: "{:url('Rbac/role_edit')}",
                data: {field:field},
                dataType: "json",
                success: function(e){
                    if (e == 1){
                        layer.msg('修改成功');
                        setTimeout(function () {
                            parent.layui.table.reload('demo'); //重载表格
                            parent.layer.close(index); //再执行关闭
                        }, 500)
                    }else{
                        layer.msg('修改失败');
                    }
                }
            });
        });
    })
</script>
</body>
</html>